package com.titleevaluation.controller;

import com.titleevaluation.entity.PersonalInfo;
import com.titleevaluation.service.PersonalInfoService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/applications/{applicationId}/personal-info")
public class PersonalInfoController {

    private final PersonalInfoService personalInfoService;

    public PersonalInfoController(PersonalInfoService personalInfoService) {
        this.personalInfoService = personalInfoService;
    }

    @PostMapping
    public ResponseEntity<PersonalInfo> createOrUpdatePersonalInfo(@PathVariable Long applicationId, @RequestBody PersonalInfo personalInfo) {
        PersonalInfo savedInfo = personalInfoService.createOrUpdatePersonalInfo(applicationId, personalInfo);
        return ResponseEntity.ok(savedInfo);
    }

    @GetMapping
    public ResponseEntity<PersonalInfo> getPersonalInfo(@PathVariable Long applicationId) {
        return personalInfoService.getPersonalInfoByApplicationId(applicationId)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }
}

